package com.dev;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.dev.R;
import com.fb.FileBrowserPage;
import java.io.File;
import static com.fb.FileBrowserPage.*;
public class MediaAudioPage extends LinearLayout implements OnClickListener{
    File[] willshow;
	int[] willshowSorted;
    int sort;
	int itemHeight;
	int intend;
	boolean[] selected;
	boolean editorCanSave;
	boolean folderOnBefore=true;
	boolean fileOnBefore;
	boolean indentItem=true;
	boolean showHidden=true;
	ViewGroup.LayoutParams itemlp;
	FileListView fileListView;
	MainActivity ma;
	ColorDrawable itemSelected=new ColorDrawable(0x77ffff00);
	HtmlBrowser hb;
	LinearLayout.LayoutParams pageLayoutParams=new LinearLayout.LayoutParams(-1,0,1f);
	int pageIndex=0;
	File file;
    MediaAudioPage(MainActivity ma){
		super(ma);
		this.ma=ma;
		setOrientation(1);
		fileListView=new FileListView();
		addView(fileListView,pageLayoutParams);
		pageIndex=0;
		itemHeight=(int)(27*ma.dp);
		itemlp=new ViewGroup.LayoutParams(-1,itemHeight);
		if(typefile==null){
			Resources res=getResources();
			typefile=BitmapFactory.decodeResource(res,R.drawable.file_type_unknown);
			folder=BitmapFactory.decodeResource(res,R.drawable.folder);
			typec=BitmapFactory.decodeResource(res,R.drawable.file_type_c);
			typecpp=BitmapFactory.decodeResource(res,R.drawable.file_type_cpp);
			typetxt=BitmapFactory.decodeResource(res,R.drawable.file_type_txt);
			typearchive=BitmapFactory.decodeResource(res,R.drawable.ic_zip_box);
			typeApk=BitmapFactory.decodeResource(res,R.drawable.format_apk);
			typeImage=BitmapFactory.decodeResource(res,R.drawable.format_picture);
			typeImageBroken=BitmapFactory.decodeResource(res,R.drawable.format_picture_broken);
			typePdf=BitmapFactory.decodeResource(res,R.drawable.ic_file_pdf);
			typeMusic=BitmapFactory.decodeResource(res,R.drawable.music);
			typeJava=BitmapFactory.decodeResource(res,R.drawable.file_type_java);
			typeSvg=BitmapFactory.decodeResource(res,R.drawable.file_type_svg);
			typeHtml=BitmapFactory.decodeResource(res,R.drawable.file_type_html);
		}
	}
	@Override
	public void onClick(View view) {
	}
	class FileListView extends MyListView {
		FileListView(){
			super(MediaAudioPage.this.ma);
            isChildCanScroll=true;
		}
		@Override
		public int getCount() {
			if(willshow==null){
				return 0;
			}else if(willshowSorted==null){
				return willshow.length;
			}else{
				return willshowSorted.length;
			}
		}
		@Override
		public View getView(View convert, int position) {
			if(willshowSorted==null){
				if(convert==null){
					FileItem fi=new FileItem();
					fi.setFile(willshow[position],position);
					return fi;
				}else if(convert instanceof FileItem){
					FileItem fi=(FileItem)convert;
					fi.setFile(willshow[position],position);
					return fi;
				}else{
					FileItem fi=new FileItem();
					fi.setFile(willshow[position],position);
					return fi;
				}
			}else{
				if(convert==null){
					FileItem fi=new FileItem();
					int ptr=willshowSorted[position];
					fi.setFile(willshow[ptr],position);
					return fi;
				}else if(convert instanceof FileItem){
					FileItem fi=(FileItem)convert;
					int ptr=willshowSorted[position];
					fi.setFile(willshow[ptr],position);
					return fi;
				}else{
					FileItem fi=new FileItem();
					int ptr=willshowSorted[position];
					fi.setFile(willshow[ptr],position);
					return fi;
				}
			}
		}
	}
	class FileItem extends LinearLayout implements OnClickListener,OnLongClickListener{
		int index;
		File file;
		IconView ivtype;
		LinearLayout linear;
		TextView name;
		TextView info;
		FileBrowserPage.Pressed pressed=new FileBrowserPage.Pressed();
		int dividerHeight=1;
		FileItem(){
			super(ma);
			setOrientation(0);
			setOnClickListener(this);
			setOnLongClickListener(this);
			setLayoutParams(itemlp);
			ivtype=new IconView(ma);
			ivtype.setOnClickListener(this);
			name=new TextView(ma);
			name.setTypeface(Typeface.MONOSPACE);
			name.setTextSize(TypedValue.COMPLEX_UNIT_DIP,11);
			name.setTextColor(0xff000000);
			name.setPadding(12,0,0,0);
			info=new TextView(ma);
			info.setTextSize(TypedValue.COMPLEX_UNIT_DIP,10);
			info.setTextColor(0xff888888);
			info.setPadding(12,0,0,0);
			linear=new LinearLayout(ma);
			linear.setOrientation(VERTICAL);
			linear.addView(name,new LayoutParams(-1,0,5));
			linear.addView(info,new LayoutParams(-1,0,4));
			addView(ivtype);
			addView(linear,new LayoutParams(0,-1,1));
		}
		void setFile(File file,int index){
			this.index=index;
			this.file=file;
			String name=file.getName();
			this.name.setText(name);
			if(selected==null){
				setBackground(pressed);
			}else if(selected[index]){
				setBackground(itemSelected);
			}else{
				setBackground(pressed);
			}
			if(name.charAt(0)=='.'){
				ivtype.setColorFilter(hidden);
			}else{
				ivtype.setColorFilter(null);
			}
			if(file.exists()){
				this.name.setTextColor(0xff000000);
				long length=file.length();
				if(length>1024*1024){
					info.setText(length/1024/1024+"MB");
				}else if(length>1024){
					info.setText(length/1024+"KB");
				}else{
					info.setText(length+"B");
				}
				int dot=name.lastIndexOf('.');
				if(dot<0){
					ivtype.setImageBitmap(typefile);
				}else{
					ivtype.setImageBitmap(typeMusic);
				}
			}else{
				ivtype.setImageDrawable(null);
				info.setText("不存在");
				this.name.setTextColor(0x77888888);
			}
		}
		@Override
		public void onClick(View view) {
			if(view==this){
				if(selected==null){
					if(file.canRead()){
						if(hb==null){
							hb=new HtmlBrowser(ma);
							MediaAudioPage.this.removeViewAt(pageIndex);
							MediaAudioPage.this.addView(hb,pageIndex,pageLayoutParams);
						}
						ma.gui.ivInto.setVisibility(View.GONE);
						ma.gui.ivQuit.setVisibility(View.VISIBLE);
						hb.webView.loadUrl("file://"+file.getAbsolutePath());
						intend=1;
					}else{
						Toast.makeText(ma,"不可读",0).show();
					}
				}else if(selected[index]){
					selected[index]=false;
					setBackground(pressed);
				}else{
					selected[index]=true;
					setBackground(itemSelected);
				}
			}else if(view==ivtype){
				if(selected==null){
					setBackground(itemSelected);
					if(willshowSorted==null){
						selected=new boolean[willshow.length];
					}else{
						selected=new boolean[willshowSorted.length];
					}
					selected[index]=true;
				}else if(selected[index]){
					selected[index]=false;
					setBackground(pressed);
					for(int i=0;true;i++){
						if(i<selected.length){
							if(selected[i]){
								break;
							}
						}else{
							selected=null;
							break;
						}
					}
				}else{
					selected[index]=true;
					setBackground(itemSelected);
				}
			}
		}
		@Override
		public boolean onLongClick(View view) {
			return true;
		}
	}
}
